﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using TierApplication;

namespace OrderMgr.OrderInfo
{
    public partial class frmStockTakeAudit : TierService.frmTemplate
    {
        public string _OrderID;

        public frmStockTakeAudit()
        {
            InitializeComponent();
            this.Text = "库存盘点单";
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            if (gFuncType == Utility.FuncType.Select)
            {
                btnAdd.Visible = false;
                btnModify.Visible = false;
                btnDeAudit.Visible = false;
                btnAudit.Visible = false;
                btnDelete.Visible = false;
                btnQuery.Location = btnAdd.Location;
            }
        }

        private void frmStockTake_Load(object sender, EventArgs e)
        {
            initForm();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ls_where = "";

            ls_where = " and a.OrderType ='" + (int)Utility.OrderType.StockTake + "'";
            string sOrderStatus = (string)txtOrderStatus.EditValue;
            if (sOrderStatus == "1")
            {
                ls_where = ls_where + " and a.OrderStatus ='1'";
            }
            if ((sOrderStatus != "0") && (sOrderStatus != "1"))
            {
                ls_where = ls_where + " and a.OrderStatus >='2'";
            }
            //if ((txtCustName.Text.Trim() != "") && (ckbOrderID.Checked))
            //    ls_where = ls_where + " and CustName  like '%" + txtCustName.Text + "%'";

            if ((txtOrderID.Text != "") && (ckbOrderID.Checked))
                ls_where = ls_where + " and a.OrderID like '%" + txtOrderID.Text + "%'";

            if ((txtDepotID.Text.Trim() != "") && (ckbDepotID.Checked))
                ls_where = ls_where + " and a.DepotID = '" + (string)txtDepotID.EditValue + "'";

            if ((txtOrderUserID.Text.Trim() != "") && (ckbOrderUserID.Checked))
                ls_where = ls_where + " and a.OrderUserID = '" + (string)txtOrderUserID.EditValue + "'";

            if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            {
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            }

            T_Order mOrder = new T_Order();
            DataSet dsCustomer = mOrder.GetOrderList(ls_where);
            gcOrder.DataSource = dsCustomer.Tables[0];
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvOrder, "订单信息");
        }



        private void gvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmStockTakeDetail fDetail = new frmStockTakeDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.StockTake;
            fDetail.ShowDialog();

            btnSearch_Click(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.GetModel(sID);
            if (mOrder.OrderStatus == "2")
            {
                MessageBox.Show("此单据已经确认，不能进行修改!");
                return;
            }

            frmStockTakeDetail fDetail = new frmStockTakeDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.StockTake;
            fDetail._OrderID = sID;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                
            }

            btnSearch_Click(null, null);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            frmStockTakeDetail fDetail = new frmStockTakeDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;            
            fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.StockTake;
            fDetail._OrderID = sID;
            fDetail.ShowDialog();
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.GetModel(sID);
            if (mOrder.OrderStatus == "2")
            {
                MessageBox.Show("此单据已经确认，不能进行修改!");
                return;
            }

            frmStockTakeDetail fDetail = new frmStockTakeDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            fDetail._AuditStatus = Utility.OrderStatus.S1Auditing;
            fDetail._OrderType = Utility.OrderType.StockTake;
            fDetail._OrderID = sID;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }

            btnSearch_Click(null, null);
            
            //if (gvOrder.RowCount < 1) return;
            //DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            //string sOrderID = dr["OrderID"].ToString();

            //T_Order mOrder = new T_Order();
            //mOrder.Audit(sOrderID, Utility.gUserID);

            //btnSearch_Click(null, null);

        }

        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {
            if (gFuncType == Utility.FuncType.Select)
            {
                if (gvOrder.RowCount < 1) return;
                DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
                _OrderID = dr["OrderID"].ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;
            
            }
            else { btnQuery_Click(null, null); }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.GetModel(sID);
            if (mOrder.OrderStatus == "2")
            {
                MessageBox.Show("此单据已经确认，不能进行删除!");
                return;
            }

            if (MessageBox.Show("是否要删除此单据，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;

            mOrder.Delete(sID);

            btnSearch_Click(null, null);
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOrderPrint_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sOrderID = dr["OrderID"].ToString();

            TdsOrder dsOrder = TOrderFunc.GetOrderPrintList(sOrderID);
            RptOrderStockTake xrptOrderBuy = new RptOrderStockTake();
            xrptOrderBuy.DataSource = dsOrder;
            xrptOrderBuy.ShowPreviewDialog();
            //string sOrderID = txtOrderID.Text;
            //if (sOrderID != "")
            //{
            //    TdsOrder dsOrder = TOrderFunc.GetOrderPrintList(sOrderID);
            //    RptOrderStockTake xrptOrderBuy = new RptOrderStockTake();
            //    xrptOrderBuy.DataSource = dsOrder;
            //    xrptOrderBuy.ShowPreviewDialog();
            //}
        }








    }
}